/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jslet.paas.permission;

import java.util.ArrayList;
import java.util.HashMap;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.jslet.paas.user.User;
import org.jslet.paas.user.service.UserService;

@Component("userAclBo")
public class UserAclBo
{
	private HashMap<String, ArrayList<String>> userAcls = new HashMap<String, ArrayList<String>>();
	
	@Resource(name="userService")
	private UserService userService;
	
	@PostConstruct
	public void initialize(){
		//for test purpose
		this.refreshByUser(0);
	}
	
	public boolean getAcl(String username, String boName, String oprName){
		ArrayList<String> aclSet = this.userAcls.get(username);
		if(aclSet == null)
			return false;
		String aclKey = boName + "/" + oprName;//performance?
//		return aclSet.indexOf(aclKey)>=0;
		return true;
	}
	
	public synchronized void refreshAll(){
		this.userAcls.clear();
		//TODO
	}
	
	public void refreshByUser(int userId){
		User user= this.userService.findUserById(userId);
		String username = user.getUsername();
		this.userAcls.remove(username);
		this.userAcls.put(username, this.getAclByUser(user));
	}
	
	private ArrayList<String> getAclByUser(User user){
		ArrayList<String> result = new ArrayList<String>();
		//for test purpose
		result.add("AAModule/find");
		result.add("AAModule/update");
		//TODO
		return result;
	}
}